﻿using System;
using System.Collections.Generic;
using System.Web;
using SpiderMaster.Net.Exceptions;

namespace SpiderMaster.AspNet
{
    public sealed partial class TradingCenter
    {
        private static List<IManagement> _CustomManagers;
        private bool InvokePlugins = false; // false by default
        private static PluginsOption _PluginsOption = PluginsOption.Normal;
        private enum PluginsOption { Override, Normal }

        /// <summary> Initializes a new Instance of the Operating System with Custom Support.
        /// </summary>
        /// <param name="CustomManagers">User Custom Managers</param>
        /// <returns></returns>
        internal static TradingCenter Initialize(List<IManagement> CustomManagers)
        {
            _CustomManagers = CustomManagers; // TODO:
            throw new NotSupportedException("Opps!! We have not Implemented this feature.");
            //return Initialize();
        }

        /// <summary> Initializes a new Instance of the Default Operating System.
        /// This can be slow as it searchs the System for compatible Plugins.
        /// </summary>
        /// <returns></returns>
        internal static TradingCenter Initialize(bool PluginsEnabledOverride)
        {
            _PluginsOption = PluginsOption.Override;
            _Instance.InvokePlugins = PluginsEnabledOverride;
            return Initialize();
        }

        /// <summary> Initializes a new Instance of the Default Operating System.
        /// </summary>
        /// <exception cref="InitializeInstanceException"/>
        /// <returns></returns>
        internal static TradingCenter Initialize()
        {
            #region Initialize Operating System

            if (_Instance.IsNotNull())
                throw new InitializeInstanceException<TradingCenter>("Operating System has allready been Initialized. Please check your code and correct as needed.");
            _Instance = GetInstance(); // DO NOT USE -- new TradingCenter();

            #endregion

            #region Initialize Plugin Management

            // DO NOT REMOVE /////////////////////////////////////////////
            //==========================================================//
            if (_PluginsOption != PluginsOption.Override)               //
                _Instance.InvokePlugins = true;                          //
            //==========================================================//
            if (_Instance.InvokePlugins)                                 //
                InitializePluginManagers();                             //
            //==========================================================//
            //////////////////////////////////////////////////////////////

            #endregion

            // if (HttpContext.Current.IsNotNull()) // TODO: Create and Use TradingCenter.Current.IsReadyToUse;
            if (TradingCenter.Current.SQLConnectionString.IsNullOrEmpty())
                if (TradingCenter.Current.SQLDBCanNotConnect)
                    new Redirect(VirtualPage.SetupInstall);

            return _Instance;
        }
    }
}
